package cn.edu.swu.miniwebsvr.handler;

import cn.edu.swu.miniwebsvr.core.Handler;
import cn.edu.swu.miniwebsvr.core.Parameter;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;

public class EchoHandler implements Handler {
    @Override
    public void handle(Parameter request, OutputStream response) {
        PrintWriter writer = new PrintWriter(response);
        Map<String, String> params = request.getAllParams();

        StringBuilder contentBuilder = new StringBuilder();
        contentBuilder.append("<!DOCTYPE html>")
                .append("<html lang=\"zh-CN\">")
                .append("<head>")
                .append("    <meta charset=\"UTF-8\">")
                .append("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
                .append("    <title>回显服务 - MiniWebSvr</title>")
                .append("    <style>")
                .append("        * { margin: 0; padding: 0; box-sizing: border-box; }")
                .append("        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f5f7fa; padding: 40px; }")
                .append("        .container { max-width: 800px; margin: 0 auto; background: white; padding: 40px; border-radius: 10px; box-shadow: 0 3px 15px rgba(0,0,0,0.1); }")
                .append("        h1 { color: #2c3e50; margin-bottom: 30px; }")
                .append("        .section { margin-bottom: 30px; padding: 20px; background: #f8f9fa; border-radius: 8px; }")
                .append("        .section h2 { color: #2c3e50; margin-bottom: 15px; border-bottom: 1px solid #ddd; padding-bottom: 10px; }")
                .append("        .param-list { list-style: none; }")
                .append("        .param-item { padding: 10px; margin-bottom: 8px; background: white; border-radius: 4px; border-left: 4px solid #3498db; }")
                .append("        .param-key { font-weight: bold; color: #2c3e50; }")
                .append("        .param-value { color: #7f8c8d; }")
                .append("        .nav { margin-top: 30px; text-align: center; }")
                .append("        .nav a { color: #3498db; text-decoration: none; margin: 0 10px; }")
                .append("        .nav a:hover { text-decoration: underline; }")
                .append("        .footer { margin-top: 30px; text-align: center; color: #7f8c8d; font-size: 0.9rem; }")
                .append("        .empty-message { text-align: center; color: #7f8c8d; padding: 20px; }")
                .append("    </style>")
                .append("</head>")
                .append("<body>")
                .append("    <div class=\"container\">")
                .append("        <h1>🔄 回显服务</h1>")
                .append("        <div class=\"section\">")
                .append("            <h2>请求详情</h2>")
                .append("            <p><strong>请求方法:</strong> ").append(request.getMethod()).append("</p>")
                .append("            <p><strong>请求URL:</strong> ").append(request.getUrl()).append("</p>")
                .append("        </div>");

        // TODO: [222024321072045/谢冈兵] 根据接收到的参数Map构建HTML列表，回显给客户端
        if (params.isEmpty()) {
            contentBuilder.append("        <div class=\"section\">")
                    .append("            <h2>接收到的参数</h2>")
                    .append("            <div class=\"empty-message\">")
                    .append("                📭 没有接收到任何参数")
                    .append("            </div>")
                    .append("        </div>");
        } else {
            contentBuilder.append("        <div class=\"section\">")
                    .append("            <h2>接收到的参数</h2>")
                    .append("            <ul class=\"param-list\">");

            for (Map.Entry<String, String> entry : params.entrySet()) {
                contentBuilder.append("<li class=\"param-item\">")
                        .append("<span class=\"param-key\">").append(entry.getKey())
                        .append(":</span> <span class=\"param-value\">").append(entry.getValue()).append("</span>")
                        .append("</li>");
            }

            contentBuilder.append("            </ul>")
                    .append("        </div>");
        }

        contentBuilder.append("        <div class=\"nav\">")
                .append("            <a href=\"/\">🏠 返回首页</a>")
                .append("            <a href=\"/time\">🕒 查看时间</a>")
                .append("            <a href=\"/echo?name=测试用户&message=这是一条测试消息\">✨ 测试参数</a>")
                .append("        </div>")
                .append("        <div class=\"footer\">")
                .append("            <p>MiniWebSvr - 轻量级Java Web服务器</p>")
                .append("        </div>")
                .append("    </div>")
                .append("</body>")
                .append("</html>");

        String content = contentBuilder.toString();

        // TODO: [222024321072045/谢冈兵] 构造完整的 HTTP 响应头
        writer.println("HTTP/1.1 200 OK");
        writer.println("Content-Type: text/html; charset=UTF-8");
        writer.println("Content-Length: " + content.getBytes().length);
        writer.println("Connection: close");
        writer.println();

        writer.println(content);
        writer.flush();
    }
}
